package com.hulk.dryad.admin.controller.gen;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hulk.dryad.admin.api.vo.gen.GenFormConfVO;
import com.hulk.dryad.admin.mapstruct.GenFormConfStruct;
import com.hulk.dryad.common.annotation.Idempotent;
import com.hulk.dryad.common.component.R;
import com.hulk.dryad.persistence.entity.GenFormConfModel;
import com.hulk.dryad.admin.service.GenFormConfService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 表单管理
 *
 * @author kim
 * @date 2019-08-12 15:55:35
 */
@Api(value = "genForm", tags = "表单管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/gen/form")
public class GenFormConfController {

	private final GenFormConfService genRecordService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param formConf 生成记录
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	public R getGenFormConfPage(Page page, GenFormConfVO formConf) {
		GenFormConfModel model = GenFormConfStruct.INSTANCE.to(formConf);
	    Page<GenFormConfModel> rpm = genRecordService.page(page, Wrappers.query(model));
		Page<GenFormConfVO> rpv =	GenFormConfStruct.INSTANCE.from(rpm);
		return R.ok(rpv);
	}

	/**
	 * 通过id查询生成记录
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Integer id) {
		GenFormConfVO vo = GenFormConfStruct.INSTANCE.from(genRecordService.getById(id));
		return R.ok(vo);
	}

	/**
	 * 通过id查询生成记录
	 * @param dsName 数据源ID
	 * @param tableName tableName
	 * @return R
	 */
	@ApiOperation(value = "通过tableName查询表单信息")
	@GetMapping("/info")
	public R form(String dsName, String tableName) {
		return R.ok(genRecordService.getForm(dsName, tableName));
	}

	/**
	 * 新增生成记录
	 * @param formConf 生成记录
	 * @return R
	 */
	@Idempotent
	@ApiOperation(value = "新增生成记录", notes = "新增生成记录")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('gen_form_add')")
	public R save(@RequestBody GenFormConfVO formConf) {
		GenFormConfModel model = GenFormConfStruct.INSTANCE.to(formConf);
		return R.ok(genRecordService.save(model));
	}

	/**
	 * 修改生成记录
	 * @param formConf 生成记录
	 * @return R
	 */
	@Idempotent
	@ApiOperation(value = "修改生成记录", notes = "修改生成记录")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('gen_form_edit')")
	public R updateById(@RequestBody GenFormConfVO formConf) {
		GenFormConfModel model = GenFormConfStruct.INSTANCE.to(formConf);
		return R.ok(genRecordService.updateById(model));
	}

	/**
	 * 通过id删除生成记录
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除生成记录", notes = "通过id删除生成记录")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('gen_form_del')")
	public R removeById(@PathVariable Integer id) {
		return R.ok(genRecordService.removeById(id));
	}

}
